#version 130

uniform sampler2D tex;

in vec2 fragmentTexCoord;

out vec4 fragColor;

void main(void){
	float cols[9];
	vec4 I = vec4(0.299, 0.587, 0.114, 0);
	vec2 offset[9];
	for(int i = -1, k = 0; i <= 1; i++)
		for(int j = -1; j <= 1; j++, k++)
			offset[k] = vec2(j,i);

	

	for(int i = 0; i < 9; i++){
		cols[i] = dot(texture(tex,fragmentTexCoord+offset[i]/textureSize(tex,0)),I);
	}
	
	float Gx = cols[0]+2*cols[1]+cols[2]-cols[6]-2*cols[7]-cols[8];
	float Gy = cols[0]+2*cols[3]+cols[6]-cols[2]-2*cols[5]-cols[8];

	float G = sqrt(Gx*Gx+Gy*Gy) > 1.0 ? 1.0 : sqrt(Gx*Gx+Gy*Gy);
	fragColor =  vec4(G,G,G, 1.0);
}
